// pages/post2/post2.js
const app = getApp(), core = require('../../utils/core'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    job: {},
    maxImage: 12,
    images: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenerEventChannel().on('jobTransfer', job => {
      let images = job.environment_images ? job.environment_images.split(',') : [];
      this.setData({job, images});
    });
    this.setData({userinfo:app.getUserinfo()});
  },
  bindResponsibilityInput: function(event) {
    this.setData({
      'job.responsibility': event.detail.value
    });
  },
  bindRequirementInput: function (event) {
    this.setData({
      'job.requirement': event.detail.value
    });
  },
  //上传多图
  uploadImage() {
    let page = this;
    let availableCount = page.data.maxImage - page.data.images.length;
    if(availableCount<1) {
      wx.showToast({title: '最多12张图片',icon:'none'});
      return;
    }
    wx.chooseImage({
      count: availableCount,
      success: res => {
        res.tempFiles.forEach(function(file){
          core.uploadFile({
            filePath: file.path,
            success(ret, response) {
              page.data.images.push(ret.data.url);
              page.setData({images: page.data.images,'job.environment_images':page.data.images.join(',')});
            }
          });
        });
      }
    });
  },
  removeImage(event) {
    let index = event.currentTarget.dataset.index;
    this.data.images.splice(index, 1);
    this.setData({images: this.data.images, 'job.environment_images':this.data.images.join(',')});
  },
  bindNextStep: function () {
    let validator = new WeValidator({
      rules: {
        responsibility: {required:true},
        // requirement: {required:true},
      },
      messages: {
        responsibility: {required:'请填写岗位职责'},
        // requirement: {required:'请填写任职要求'},
      }
    });
    // 图片非必传，  注释掉
    // let userinfo = app.getUserinfo();
    // if(userinfo.group_id==3) {
    //   validator.addRules({
    //     rules: {
    //       environment_images: {required:true}
    //     },
    //     messages: {
    //       environment_images: {required:'至少上传一张工作环境图'}
    //     }
    //   });
    // }
    // if(!validator.checkData(this.data.job)) {
    //   return;
    // }
    wx.navigateTo({url: '/pages/post3/post3', success: res => {
      res.eventChannel.emit("jobTransfer", this.data.job);
    }});
  },
});
